from rest_framework import serializers

class UserProfileUpdateForms(serializers.Serializer):
    email = serializers.EmailField(required=False)
    nick_name = serializers.CharField(max_length=32, required=False)
    avatar = serializers.CharField(max_length=256, required=False)

    def validate_email(self, value):
        if value:
            from users.models import User
            if User.objects.filter(email=value).exists():
                raise serializers.ValidationError("该邮箱已被使用")
        return value

class ChangePasswordForms(serializers.Serializer):
    old_password = serializers.CharField(required=True)
    new_password = serializers.CharField(required=True, min_length=6)

    def validate_new_password(self, value):
        if len(value) < 6:
            raise serializers.ValidationError("密码长度不能小于6位")
        return value
